import {NextFunction, Request, Response} from "express";
import ActivityService from "../services/ActivityService";
import ResponseDTO from "@/dto/ResponseDTO";

class ActivityController {
    /** 创建活动 */
    public async createActivity(req: Request, res: Response, next: NextFunction) {
        try {
            const activityParams = req.body;
            const createdActivity = await ActivityService.createActivity(activityParams);
            res.locals.data = createdActivity;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 获取活动列表 */
    public async getActivityList(req: Request, res: Response, next: NextFunction) {
        try {
            const listParams = {
                pageSize: Number(req.query?.pageSize || 10),
                pageIndex: Number(req.query?.pageIndex || 1),
                activityName: String(req.query?.activityName || ""),
                startTime: Number(req.query?.startTime || 0),
            };
            const activityList = await ActivityService.getActivityList(listParams);
            res.locals.data = activityList;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 更新活动信息 */
    public async updateActivity(req: Request, res: Response, next: NextFunction) {
        try {
            const activityParams = req.body;
            if (!activityParams?.activityId) {
                res.send(ResponseDTO.error(100, "参数错误，activityId不能为空"));
                return;
            }
            const updatedActivity = await ActivityService.updateActivity(activityParams);
            res.locals.data = updatedActivity;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 删除活动信息 */
    public async deleteActivity(req: Request, res: Response, next: NextFunction) {
        try {
            const {activityId} = req.params;
            if (!activityId) {
                res.send(ResponseDTO.error(100, "参数错误，activityId不能为空"));
                return;
            }
            await ActivityService.deleteActivity(activityId);
            res.locals.data = true;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }

    /** 获取活动信息 */
    public async getActivityById(req: Request, res: Response, next: NextFunction) {
        try {
            const {activityId} = req.params;
            if (!activityId) {
                res.send(ResponseDTO.error(100, "参数错误，activityId不能为空"));
                return;
            }
            const activity = await ActivityService.getActivityById(activityId);
            res.locals.data = activity;
            next();
        } catch (error) {
            res.locals.error = error;
            next();
        }
    }
}

export default new ActivityController();
